package nimble.json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastjsonUtils {

    public static String toJSONString(Object object)
    {
        return JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.UseISO8601DateFormat);
    }

    @SuppressWarnings("rawtypes")
    public static Map toMap(JSONObject jsonObject)
    {
        return JSON.toJavaObject(jsonObject, Map.class);
    }

    @SuppressWarnings("rawtypes")
    public static List toList(JSONArray jsonArray)
    {
        List<Object> list=new ArrayList<Object>();
        for(Object object:jsonArray)
        {
            list.add(object);
        }
        return list;
    }

    public static JSONObject parseObject(String jsonString)
    {
        return JSON.parseObject(jsonString);
    }

    @SuppressWarnings("rawtypes")
    public static JSONObject parseObject(Map map)
    {
        return JSON.parseObject(JSON.toJSONString(map,SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.UseISO8601DateFormat));
    }

    @SuppressWarnings("rawtypes")
    public static JSONArray parseArray(List list)
    {
        return JSON.parseArray(JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.UseISO8601DateFormat));
    }
}

